package cn.iqianye.app.qmc

import java.io.DataOutputStream
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

object NetUtils {
    @Throws(Exception::class)
    fun post(data: String, url: String): String? {
        val getUrl = URL(url)
        val con: HttpURLConnection = getUrl.openConnection() as HttpURLConnection
        con.setReadTimeout(3000)
        con.setConnectTimeout(5000)
        con.addRequestProperty("Charset", "UTF-8")
        con.addRequestProperty("User-Agent", GlobalVal.ua)
        con.setRequestMethod("POST")
        con.setDoOutput(true)
        con.setDoInput(true)
        con.connect()
        //DataOutputStream流
        val out = DataOutputStream(con.getOutputStream())
        //将要上传的内容写入流中
        out.writeBytes(data)
        //刷新、关闭
        out.flush()
        out.close()
        val s: String?
        val code: Int = con.getResponseCode()
        if (code == 200 || code == 201 || code == 202) {
            val `is`: InputStream = con.getInputStream()
            s = Utils.readInputStream(`is`)
            `is`.close()
        } else {
            val `is`: InputStream = con.getErrorStream()
            s = Utils.readInputStream(`is`)
            `is`.close()
        }
        return s
    }
}
